package com.example.javaservice.service;

import com.example.javaservice.mapper.PostMapper;
import com.example.javaservice.pojo.Post;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PostService {
    private final PostMapper postMapper;

    public PostService(PostMapper postMapper) {
        this.postMapper = postMapper;
    }

    public boolean createPost(Post post) {
        return postMapper.insert(post) > 0;
    }
    public List<Post> findAllApproved() {
        return postMapper.findAllApproved();
    }
    public boolean approvePost(Integer postId) {
        return postMapper.updateStatus(postId, "approved") > 0;
    }
    /** ✅ 管理员查看所有帖子（含 pending、approved、rejected） */
    public List<Post> findAll() {
        return postMapper.findAll();
    }
    public boolean rejectPost(Integer postId) {
        return postMapper.updateStatus(postId, "rejected") > 0;
    }
    public Post findById(Integer id) {
        return postMapper.findById(id);
    }
    public List<Post> searchPosts(String keyword) {
        return postMapper.searchPosts(keyword);
    }

    @Transactional
    public void increaseViews(Integer id) {
        postMapper.increaseViews(id);
    }

}
